﻿#region 命名空间

using System;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;
using WinnerCMS.Model.Enum;

#endregion

namespace WinnerCMS.WebUI.Admin.Gather
{
    /// <summary>
    ///     ArticleGather_Add 的摘要说明。
    /// </summary>
    [Access("272001,272003")]
    public partial class Add : WebFactory.PageBase
    {
        private readonly GatherManage _myBLL = new GatherManage();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._myBLL.Copy(this.ddlGather);
                this.btnGatherAdd.Attributes.Add("onclick", "return CheckForm();");
                if (Request.QueryString["id"] != null) //修改界面
                {
                    new CategoryManage().BindDDL(this.ddlArticleCategory, BindOption.NoLink, "<请选择栏目>",
                        byte.Parse(Request["ModelId"]));
                    Bind(Request.QueryString["id"]);
                }
                else
                {
                    new CategoryManage().BindDDL(this.ddlArticleCategory, BindOption.NoLink, "<请选择栏目>",
                        byte.Parse(Request["ModelId"]));
                }
            }
        }

        protected void btnGatherAdd_Click(object sender, EventArgs e)
        {
            var model = new WinnerCMS.Model.Gather();
            if (Request.QueryString["ID"] != null)
            {
                model.Id = int.Parse(Request.QueryString["ID"]);
                //model = _myBLL.GetInfo(int.Parse(Request.QueryString["ID"]));
            }

            model.Name = this.txtGatherName.Text;
            model.StartUrl = this.txtStartUrl.Text;
            model.CategoryArea = this.txtCategoryArea.Text;
            model.CategoryPageNextUrl = this.txtCategoryPageNextUrl.Text;

            model.ContentPageUrl = this.txtContentPageUrl.Text;
            model.FullArticleUrl = this.txtFullArticleUrl.Text;
            model.IgnoreUrl = this.txtIgnoreUrl.Text;
            model.ContentTitle = this.txtContentTitle.Text;
            model.Content = this.txtContent.Text;
            model.ContentPageNextUrl = this.txtContentPageNextUrl.Text;
            model.ComeFrom = this.txtFrom.Text;
            model.Author = this.txtAuthor.Text;
            model.UpdateTime = this.txtUpdateTime.Text;
            model.Filter = this.txtFilter.Text;
            model.CanDownImg = this.RBL_DownImg.Items[0].Selected;
            model.CategoryId = int.Parse(this.ddlArticleCategory.SelectedValue);
            model.IsChecked = this.RBL_Pass.Items[0].Selected;
            model.Replace = this.txtReplace.Text;
            model.CanDrawWaterMark = this.RBL_DrawWaterMark.Items[0].Selected;


            var res = this._myBLL.Save(model);
            if (string.IsNullOrEmpty(res.Errors))
            {
                MsgBox.Message("保存成功!", "Main.aspx?ModelId=" + Request["ModelId"], this.UpdatePanel1);
            }
            else
            {
                MsgBox.Message(res.Errors, this.UpdatePanel1);
            }
        }


        private void Bind(string ID)
        {
            var model = this._myBLL.GetInfo(int.Parse(ID));

            this.txtGatherName.Text = model.Name;
            this.txtStartUrl.Text = model.StartUrl;
            this.txtCategoryArea.Text = model.CategoryArea;
            this.txtCategoryPageNextUrl.Text = model.CategoryPageNextUrl;

            this.txtContentPageUrl.Text = model.ContentPageUrl;
            this.txtFullArticleUrl.Text = model.FullArticleUrl;
            this.txtIgnoreUrl.Text = model.IgnoreUrl;
            this.txtContentTitle.Text = model.ContentTitle;
            this.txtContent.Text = model.Content;
            this.txtContentPageNextUrl.Text = model.ContentPageNextUrl;
            this.txtFrom.Text = model.ComeFrom;
            this.txtAuthor.Text = model.Author;
            this.txtUpdateTime.Text = model.UpdateTime;
            this.txtFilter.Text = model.Filter;
            this.RBL_DownImg.Items[0].Selected = model.CanDownImg;
            this.RBL_DownImg.Items[1].Selected = !this.RBL_DownImg.Items[0].Selected;
            this.RBL_Pass.Items[0].Selected = model.IsChecked;
            this.RBL_Pass.Items[1].Selected = !this.RBL_Pass.Items[0].Selected;
            this.RBL_DrawWaterMark.Items[0].Selected = model.CanDrawWaterMark;
            this.RBL_DrawWaterMark.Items[1].Selected = !this.RBL_DrawWaterMark.Items[0].Selected;
            this.ddlArticleCategory.SelectedValue = model.CategoryId.ToString();
            this.txtReplace.Text = model.Replace;
        }


        protected void ddlGather_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(this.ddlGather.SelectedValue) > 0)
            {
                Bind(this.ddlGather.SelectedValue);

                this.txtGatherName.Text = string.Empty;
                this.txtStartUrl.Text = string.Empty;
            }
        }
    }
}